<?php

class ContactController extends MyController
{

    public function actionIndex()
    {
        $model             = new Contact('search');
        if (isset($_GET['Contact']))
            $model->attributes = $_GET['Contact'];
        $this->render('admin', array(
            'model' => $model,
        ));
        //$this->render('index');
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     * array('allow', // allow authenticated users to access all actions
      'users' => array('@'),
      ),

      public function accessRules(){
      return CMap::mergeArray(parent::accessRules(),array(
      array('allow', // allow authenticated users to access all actions
      'actions'=>array('admin','index'),
      'users' => array('dev@webmaster.com.sg'),
      ),
      ));
      }
     * */
    // Uncomment the following methods and override them if needed
    /*
      public function filters()
      {
      // return the filter configuration for this controller, e.g.:
      return array(
      'inlineFilterName',
      array(
      'class'=>'path.to.FilterClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }

      public function actions()
      {
      // return external action classes, e.g.:
      return array(
      'action1'=>'path.to.ActionClass',
      'action2'=>array(
      'class'=>'path.to.AnotherActionClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }
     */

    /**
     * @param $id
     * @throws CHttpException
     */
    public function actionView($id)
    {
        $model = Contact::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');

        if ($model->status == Contact::STATUS_NEW)
            Contact::model()->updateByPk($id, array('status' => Contact::STATUS_DONE));
        $this->render('view', array(
            'model' => $model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer $id the ID of the model to be loaded
     * @return Contact the loaded model
     * @throws CHttpException
     */
    public function loadModel($id)
    {
        $model = Contact::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function actionEdit()
    {
        $this->checkAccess();
        if (isset($_GET['id']) && $_GET != null)
        {
            $model = Contact::model()->findByPk($_GET['id']);

            $status = $model->status;

            if (isset($_POST['Contact']))
            {
                $model->attributes = $_POST['Contact'];
                if ($model->validate() && $model->save())
                {
                    Yii::app()->user->setFlash('success', 'Edit successful');

                    if ($status != Contact::STATUS_DONE AND $model->status == RequestBooking::STATUS_DONE)
                    {
                        //Change status value from 1 to 2 in DB
                    }
                    Yii::app()->request->redirect(Yii::app()->createUrl('request/index'));
                }
                else
                {
                    Yii::app()->user->setFlash('error', 'Edit failed!');
                }
            }
        }
        $this->render('_form', array('model' => $model));
    }

}
